package com.dq.controller.index;

import com.dq.annotation.aop.DataFilter;
import com.dq.exception.customException.AdminException;
import com.dq.controller.op.opmerchant.in.query.AboutUsQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;

import com.dq.domain.sys.index.AboutUs;
import com.dq.domain.sys.section.SysSection;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author ----
 */
@Api(description = "关于我们管理")
@RestController
@RequestMapping("/sys/aboutUs")
public class AboutUsController extends AllService {
    @ApiOperation("根据条件检索")
    @PostMapping("/list")
    @RequiresPermissions("sys:aboutUs:list")
    @DataFilter(deptAlias = "org")
    public Vo list(@RequestBody AboutUsQuery aboutUsQuery) throws AdminException {
        aboutUsQuery.format();
        Map<String, Object> query = aboutUsQuery.parseMap();
        if (StringUtils.isNotBlank(aboutUsQuery.getOrgName())) {
            query.put("org_likename", aboutUsQuery.getOrgName());
            query.remove("orgName");
        }
        List<AboutUs> aboutUses = dao.find(dao.queryAdminHql(AboutUs.class, query), query);
        if (aboutUses != null && !aboutUses.isEmpty()) {
            return new Vo(aboutUses.get(0));
        }
        return new Vo(null);
    }


    @ApiOperation("新增关于我们")
    @PostMapping("/add/{orgId}")
    @RequiresPermissions("sys:aboutUs:add")
    public VO add(@RequestBody AboutUs aboutUs, @PathVariable Long orgId) {
        aboutUs.setOrg(dao.get(SysSection.class, orgId));
        aboutUs.setCreateUser(shiroService.getUser());
        aboutUsRepository.save(aboutUs);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("编辑关于我们")
    @PostMapping("/update/{aboutUsId}")
    @RequiresPermissions("sys:aboutUs:update")
    public VO update(@RequestBody AboutUs updateAboutUs, @PathVariable @ApiParam(value = "轮播id") Long aboutUsId) throws
            AdminException {
        AboutUs aboutUs = dao.get(AboutUs.class, aboutUsId);
        if (aboutUs == null) {
            throw new AdminException("未找到此banner");
        }
        BeanUtils.copyProperties(updateAboutUs, aboutUs, BeanNullUtil.getNullPropertyNames(updateAboutUs));
        aboutUs.setUpdateUser(shiroService.getUser());
        aboutUsRepository.save(aboutUs);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("删除关于我们")
    @PostMapping("/delete/{aboutUsId}")
    @RequiresPermissions("sys:aboutUs:delete")
    public VO delete(@PathVariable @ApiParam(value = "轮播id") Long aboutUsId) throws AdminException {
        AboutUs aboutUs = dao.get(AboutUs.class, aboutUsId);
        if (aboutUs == null) {
            throw new AdminException("未找到此banner");
        }
        aboutUs.setDeleted(true);
        aboutUsRepository.delete(aboutUs);
        return new Vo(HttpStatus.OK);
    }

}
